package ovm.gui.tools;

import com.vaadin.ui.*;
import ovm.entity.EmployeeEntity;
import ovm.entity.ScheduleEntity;
import ovm.entity.StudentEntity;
import ovm.gui.views.AbstractEntityListView;
import ovm.gui.views.EntityListViewFactory;

import static ovm.application.MainApplication.*;

public class ToolsPanel extends AbstractToolsPanel {

    private HorizontalLayout layout;
    private Button goNextButton;
    private Button goPreviousButton;
    private Button refreshButton;
    private GridLayout gridLayout;

    private TextField searchTextField;
    private ComboBox categorySelect;
    private Button searchButton;

    public ToolsPanel () {
        super();

        setCaption("Панель инструментов и навигации");

        gridLayout = new GridLayout(2, 1);

        layout = new HorizontalLayout();

        goNextButton = new Button("Вперед", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                navigator.goNextView();
            }
        });

        goPreviousButton = new Button("Назад", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                navigator.goPreviousView();
            }
        });

        refreshButton = new Button ("Обновить приложение", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                navigator.draw();
            }
        });

        layout.addComponent(goPreviousButton);
        layout.addComponent(goNextButton);
        layout.addComponent(refreshButton);

        gridLayout.setWidth("1150px");
        gridLayout.setSpacing(true);

        gridLayout.addComponent(layout, 0, 0);

        searchTextField = new TextField();
        categorySelect = new ComboBox();
        categorySelect.addItem("Студенты");
        categorySelect.addItem("Преподаватели");
        categorySelect.addItem("Расписание");
        categorySelect.setNullSelectionAllowed(false);
        categorySelect.setTextInputAllowed(false);

        searchButton = new Button("Найти", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (categorySelect.getValue() != null && searchTextField.getValue() != null) {
                    AbstractEntityListView searchResult = null;
                    if (categorySelect.getValue().equals("Студенты")) {
                        searchResult = EntityListViewFactory.createEntityListView(StudentEntity.class, false);
                        searchResult.setFilter("lastName", searchTextField.getValue());
                    } else if (categorySelect.getValue().equals("Преподаватели")) {
                        searchResult = EntityListViewFactory.createEntityListView(EmployeeEntity.class, false);
                        searchResult.setFilter("lastName", searchTextField.getValue());
                    } else if (categorySelect.getValue().equals("Расписание")) {
                        searchResult = EntityListViewFactory.createEntityListView(ScheduleEntity.class, false);
                        searchResult.setFilter("studyGroup.name", searchTextField.getValue());
                    }
                    searchResult.setCaption("Результаты поиска");
                    navigator.setMainView(searchResult);
                }
            }
        });

        HorizontalLayout searchLayout = new HorizontalLayout();

        searchLayout.addComponent(searchButton);
        searchLayout.addComponent(searchTextField);
        searchLayout.addComponent(categorySelect);

        gridLayout.addComponent(searchLayout, 1, 0);
        gridLayout.setComponentAlignment(gridLayout.getComponent(1, 0), Alignment.TOP_RIGHT);

        addComponent(gridLayout);
    }

    public void setNextButtonStatus (boolean status) {
        goNextButton.setEnabled(status);
    }

    public void setPreviousButtonState (boolean status) {
        goPreviousButton.setEnabled(status);
    }

}
